home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / specfunc / elljac.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-18  |  2.9 KB  |  109 lines

  1. /* specfunc/elljac.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* Author:  G. Jungman */
  21.  
  22. #include <config.h>
  23. #include <gsl/gsl_math.h>
  24. #include <gsl/gsl_errno.h>
  25. #include <gsl/gsl_sf_pow_int.h>
  26. #include <gsl/gsl_sf_elljac.h>
  27.  
  28.  
  29. /* See [Thompson, Atlas for Computing Mathematical Functions] */
  30.  
  31.  
  32. int
  33. gsl_sf_elljac_e(double u, double m, double * sn, double * cn, double * dn)
  34. {
  35.   if(fabs(m) > 1.0) {
  36.     *sn = 0.0;
  37.     *cn = 0.0;
  38.     *dn = 0.0;
  39.     GSL_ERROR ("|m| > 1.0", GSL_EDOM);
  40.   }
  41.   else if(fabs(m) < 2.0*GSL_DBL_EPSILON) {
  42.     *sn = sin(u);
  43.     *cn = cos(u);
  44.     *dn = 1.0;
  45.     return GSL_SUCCESS;
  46.   }
  47.   else if(fabs(m - 1.0) < 2.0*GSL_DBL_EPSILON) {
  48.     *sn = tanh(u);
  49.     *cn = 1.0/cosh(u);
  50.     *dn = *cn;
  51.     return GSL_SUCCESS;
  52.   }
  53.   else {
  54.     int status = GSL_SUCCESS;
  55.     const int N = 16;
  56.     double   a[16];
  57.     double   b[16];
  58.     double   c[16];
  59.     double phi[16];
  60.     double psi[16]; /* psi[i] := phi[i] - Pi 2^{i-1} */
  61.     double two_N;
  62.     int n = 0;
  63.  
  64.     a[0] = 1.0;
  65.     b[0] = sqrt(1.0 - m);
  66.     c[0] = sqrt(m);
  67.  
  68.     while( fabs(c[n]) > 4.0 * GSL_DBL_EPSILON) {
  69.       a[n+1] = 0.5 * (a[n] + b[n]);
  70.       b[n+1] = sqrt(a[n] * b[n]);
  71.       c[n+1] = 0.5 * (a[n] - b[n]);
  72.       if(n >= N - 2) {
  73.         status = GSL_EMAXITER;
  74.     c[N-1] = 0.0;
  75.     break;
  76.       }
  77.       ++n;
  78.     }
  79.  
  80.     --n;
  81.     two_N = (double)(1 << n ); /* 2^n */  /* gsl_sf_pow_int(2.0, n); */
  82.     phi[n] = two_N * a[n] * u;
  83.     psi[n] = two_N * (a[n]*u - 0.5*M_PI);
  84.  
  85.     while(n > 0) {
  86.       const double psi_sgn = ( n == 1 ? -1.0 : 1.0 );
  87.       const double phi_asin_arg = c[n] * sin(phi[n])/a[n];
  88.       const double psi_asin_arg = c[n]/a[n] * psi_sgn * sin(psi[n]);
  89.       const double phi_asin = asin(phi_asin_arg);
  90.       const double psi_asin = asin(psi_asin_arg);
  91.       phi[n-1] = 0.5 * (phi[n] + phi_asin);
  92.       psi[n-1] = 0.5 * (psi[n] + psi_asin);
  93.       --n;
  94.     }
  95.  
  96.     *sn = sin(phi[0]);
  97.     *cn = cos(phi[0]);
  98.     {
  99.       /* const double dn_method_1 = *cn / cos(phi[1] - phi[0]); */
  100.       const double dn_method_2 = sin(psi[0])/sin(psi[1] - psi[0]);
  101.       *dn = dn_method_2;
  102.       /* printf("%18.16g  %18.16g\n", dn_method_1, dn_method_2); */
  103.     }
  104.  
  105.     return status;
  106.   }
  107. }
  108.  
  109.